<template>
  <div class="develop-header">
    <div class="develop-header__title">{{title || ''}}</div>
    <div class="develop-header__toolbar">
      <Space>
        <Select v-model="aitype" style="width: 100px" @on-change="getList()" clearable>
          <Option
            v-for="item in TYPE_LIST"
            :value="item.value"
            :key="item.value"
            >{{ item.label }}</Option
          >
        </Select>
        <!-- <Select v-model="model" style="width: 100px">
          <Option
            v-for="item in POST_LIST"
            :value="item.value"
            :key="item.value"
            >{{ item.label }}</Option
          >
        </Select> -->
        <Button type="primary" @click="addProject()" icon="md-add">新增</Button>
        <!-- <Button type="default" @click="userSelect" icon="md-cloud-download"
          >导出</Button
        >
        <Button type="default" @click="userSelect" icon="md-cloud-upload"
          >导入</Button
        > -->
      </Space>
    </div>
  </div>
  <div class="develop-content" v-if="dataList.length" v-loading="loading">
    <div class="develop-content-item" v-for="(item, i) in dataList" :key="i" @click="handleItemClick(item.aitype, item.id)">
      <div class="header">
        <div class="left">
          <div class="title">{{ item.name }}</div>
          <div class="description">{{ item.description }}</div>
        </div>
        <div class="right">
          <!-- <img :src="item.icon" class="icon" alt="develop icon" /> -->
          <Dropdown>
          <Icon type="ios-more" />
            <template #list>
                <DropdownMenu>
                    <DropdownItem @click.stop="handleRemove(item, i)">
                      <text-button style="width: 100%" type="del" >删除</text-button>
                    </DropdownItem>
                </DropdownMenu>
            </template>
        </Dropdown>
        </div>
      </div>
      <div class="type type-blue">{{ TYPE_NAME[item.aitype] }}</div>
      <div class="datetime">{{ item.creater }}·最近编辑{{ formatToDateTime(item.updatetime || item.createtime) }}</div>
    </div>
  </div>
  <div class="no-content" v-else>
    <Icon type="ios-folder-open-outline" size="60" />
    <div class="tip">还没有应用，快去创建一个吧！</div>
  </div>
  <Page
    v-if="dataList"
    v-model="pageIndex"
    :page-size="pageSize"
    :total="total"
    show-elevator
    show-total
    @on-change="getList()"
  />

  <AddProjectDialog ref="addProjectRef" :kbtype="typeId" @submit="addProjectSuccess" />
</template>

<script setup>
import { ref, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
import { agentList, deleteAgent } from '@/api/modules/agent';
import { deleteFlow } from '@/api/modules/flow';

import { formatToDateTime } from '@/util/dateUtil';
import DefaultImg from '@/assets/imgs/default.png'

import { TYPE_NAME, TYPE_ENUM } from './components/constant';
import AddProjectDialog from './components/AddProjectDialog.vue';

const props = defineProps({
  title: {
    type: String
  },
  typeId: {
    type: String,
  }
})
const { proxy } = getCurrentInstance();


const TYPE_LIST = [
  {
    label: '工作流',
    value: TYPE_ENUM.FLOW,
  },
  {
    label: '智能体',
    value: TYPE_ENUM.AGENT,
  },
];
const POST_LIST = [
  {
    label: '全部',
    value: '',
  },
  {
    label: '已发布',
    value: 1,
  },
  {
    label: '最近打开',
    value: 2,
  },
];
// 分页
const pageIndex = ref(1);
const pageSize = 20;
const total = ref(0);
const loading = ref(false);
// 项目列表
const dataList = ref([]);
const aitype = ref(undefined)
// 路由
const router = useRouter();

// 获取列表
const getList = async(reset) => {
  if (reset) {
    pageIndex.value = 1;
  }
  loading.value = true;
  try {
    const { data} = await agentList({
      query: {
        p: pageIndex.value,
        ps: pageSize,
      },
      data: {
        kbtype: props.typeId || undefined,
        aitype: aitype.value
      },
    });
    dataList.value = data.topicList;
    total.value = data.totalElements;
  }finally{
    loading.value = false;
  }
};

const init = () => {
  getList()
}

// 新增项目
const addProjectRef = ref(null);
const addProject = () => {
  addProjectRef.value?.init();
};
const addProjectSuccess = () => {
  // 重置
  getList(true)
};

const handleItemClick = (type, id) => {
  if (type == TYPE_ENUM.AGENT) {
    router.push('/app?id='+ id)
    return;
  } 
  router.push('/workflow?id='+ id)
}

// 删除项目
const handleRemove = (item, index) => {
  proxy.$Modal.confirm({
    mask: false,
    title: '警告',
    content: `<p>是否确认删除${item.name}?</p>`,
    okText: '确认',
    cancelText: '取消',
    onOk: async () => {
      await (item.aitype == TYPE_ENUM.AGENT ? deleteAgent : deleteFlow)({
        data: { id: item.id },
      });
      proxy.$Message.success('确认移除');
      getList();
    },
  });
};

const stopEvent = () => {

}
 
defineExpose({
  init
})

</script>

<style lang="scss" scoped>
@media screen and (max-width: 599px) {
  .develop-content {
    grid-template-columns: repeat(1, 1fr);
  }
}
@media screen and (min-width: 600px) and (max-width: 1199px) {
  .develop-content {
    grid-template-columns: repeat(2, 1fr);
  }
}
@media screen and (min-width: 1200px) and (max-width: 1499px) {
  .develop-content {
    grid-template-columns: repeat(3, 1fr);
  }
}
@media screen and (min-width: 1500px) and (max-width: 1799px) {
  .develop-content {
    grid-template-columns: repeat(4, 1fr);
  }
}
@media screen and (min-width: 1800px) {
  .develop-content {
    grid-template-columns: repeat(5, 1fr);
  }
}
.develop-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  padding: 10px;
  &__title {
    font-size: 18px;
    font-weight: 600;
  }
}
.develop-content {
  background-color: #fff;
  padding: 10px;
  margin-top: 10px;
  display: grid;
  grid-gap: 1.25rem;
  max-height: 100%;
  overflow-y: auto;
}
.develop-content-item {
  height: 140px;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid rgba(82, 100, 154, 0.13);
  cursor: pointer;
  &.checkbox {
    height: 160px;
  }
  .header {
    display: flex;
    justify-content: space-between;
    padding-bottom: 10px;
  }
  .icon {
    width: 60px;
    height: 60px;
    border-radius: 12px;
  }
  .title {
    font-size: 16px;
  }
  .description {
    font-size: 13px;
    color: #999;
    width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }
  .datetime {
    font-size: 12px;
    color: #999;
  }
  .type {
    width: 50px;
    text-align: center;
    padding: 2px;
    border-radius: 2px;
    font-size: 12px;
    background-color: rgba(87, 104, 161, 0.08);
    color: rgba(15, 21, 40, 0.82);
    &.type-blue {
      background-color: rgba(171, 181, 255, 0.3);
      color: rgb(81, 71, 255);
    }
  }
}
.check-project-btns {
  padding-top: 10px;
  /* text-align: right; */
  .btn {
    margin-right: 10px;
  }
}

.no-content {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  color: #666;
  background-color: #fff;
}
</style>
